package myta.bean;

import java.util.Map;

import javax.faces.component.html.HtmlInputText;

import myta.base.FormDataBase;
import myta.model.DataRow;
import myta.model.DataTable;
import myta.service.Command;
import myta.service.SQLexec;

public class MapelData extends FormDataBase{
	private HtmlInputText txtidmapel;
	private HtmlInputText txtnamamapel;
	
	@Override
	public void init() throws Exception {
		formName = "mapel";
		
		txtidmapel = new HtmlInputText();
		txtnamamapel = new HtmlInputText();
	}

	@Override
	public void generateID() throws Exception {
		String sql = "call idmapel()";
		Command command = SQLexec.makeCommand(sql);
		DataTable datatable = command.executeQuery();
		DataRow datarow = datatable.getRow(0);
		txtidmapel.setValue(datarow.getString("id"));
	}

	@Override
	public void getRequestData(Map<String, String> map) throws Exception {
		if(map.containsKey("idmapel")){
			String idmapel = map.get("idmapel");
			
			String sql = "select * from mapel where idmapel = ? order by idmapel desc";
			Command command = SQLexec.makeCommand(sql);
			command.setObject(1, idmapel);
			DataTable datatable = command.executeQuery();
			DataRow datarow = datatable.getRow(0);
			
			txtidmapel.setValue(datarow.getString("idmapel"));
			txtnamamapel.setValue(datarow.getString("namamapel"));
		}
	}

	@Override
	public boolean simpan() throws Exception {
		boolean boo = false;
		
		String sql = "INSERT INTO mapel(idmapel,namamapel) VALUES(?,?);";
		Command command = SQLexec.makeCommand(sql);
		command.setObject(1, txtidmapel.getValue().toString());
		command.setObject(2, txtnamamapel.getValue().toString());
		boo = command.executeUpdate();
		
		if(boo){
			return true;
		}else{
			return false;
		}		
	}

	@Override
	public boolean ubah() throws Exception {
		boolean boo = false;
		
		String sql = "UPDATE mapel SET namamapel=? where idmapel = ?;";
		Command command = SQLexec.makeCommand(sql);
		command.setObject(1, txtnamamapel.getValue().toString());
		command.setObject(2, txtidmapel.getValue().toString());
		boo = command.executeUpdate();
		
		if(boo){
			return true;
		}else{
			return false;
		}
	}

	public HtmlInputText getTxtidmapel() {
		return txtidmapel;
	}

	public void setTxtidmapel(HtmlInputText txtidmapel) {
		this.txtidmapel = txtidmapel;
	}

	public HtmlInputText getTxtnamamapel() {
		return txtnamamapel;
	}

	public void setTxtnamamapel(HtmlInputText txtnamamapel) {
		this.txtnamamapel = txtnamamapel;
	}
}
